package com.dlshouwen.core.base.videoconvert;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import com.dlshouwen.core.base.config.CONFIG;

import net.sf.json.JSONObject;

public class UploadFileUtil{

	public static JSONObject upload(String fname, InputStream input){
        String IMGTYPE = "jpg_jpeg_bmp_gif_png_tiff_JPG_JPEG_PNG_BMP_GIF_TIFF";
        String VIDEOTYPE = "avi_wmv_mp4_mov_flv_rmvb_rm_3gp_wmv9";
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("state", "FAILD");
        
        //文件名+后缀
//        String fname = request.getParameter("filename");
        //文件名不+后缀
        String fplainName = null;
        //文件后缀
        String fsuf = null;
        try {
            fsuf = fname.substring(fname.lastIndexOf(".")+1);
            fplainName = fname.substring(0, fname.lastIndexOf("."));
        } catch(Exception e) {
        	e.printStackTrace();
            return jsonObj;
        }
        //实际路径
        String sourcePath = CONFIG.SOURCE_REAL_PATH;
        //返回的访问路径名+文件名
        String filePath = "";
        //视频文件标识和封面路径
        boolean ifVideo = false;
        String coverPath = "";
        
        if(null != fsuf && !"".equals(fsuf)) {
            if(IMGTYPE.contains(fsuf)) {
                sourcePath += CONFIG.UPLOAD_PIC_PATH;
                filePath += CONFIG.UPLOAD_PIC_PATH;
            }else if(VIDEOTYPE.contains(fsuf)) {
                sourcePath += CONFIG.UPLOAD_VIDEO_PATH;
                filePath += CONFIG.UPLOAD_VIDEO_PATH;
                ifVideo = true;
            }else {
                sourcePath += CONFIG.UPLOAD_FILE_PATH;
                filePath += CONFIG.UPLOAD_FILE_PATH;
            }
        }else {
            sourcePath += CONFIG.UPLOAD_FILE_PATH;
            filePath += CONFIG.UPLOAD_FILE_PATH;
        }
        
        
        File file = new File(sourcePath);  
        if(!file.exists()){  
            file.mkdirs();  
        }  
        //以时间给文件重命名
        
        String newName = fplainName + "_" +new Date().getTime() + ".";
        if(ifVideo) {
            fname = sourcePath + "/" +  newName + fsuf;
            coverPath = filePath + "/" + newName + "jpg";
            newName += "flv";
        }else {
            newName += fsuf;
            fname = sourcePath + "/" +  newName;
        }
        filePath += "/" +  newName;
        
        FileOutputStream fos = null;
        try {
	        fos = new FileOutputStream(fname);  
	  
	        int size = 0;  
	        byte[] buffer = new byte[1024];  
	        while ((size = input.read(buffer,0,1024)) != -1) {  
	            fos.write(buffer, 0, size);  
	        }  
        } catch (Exception e) {
			e.printStackTrace();
			return jsonObj;
		} finally {
			try {
				if(null != fos) {
					fos.close();
				}
				if(null != input) {
					input.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}  
	        
		}
        jsonObj.put("state", "SUCCESS");
        jsonObj.put("fpath", filePath);
        jsonObj.put("fname", newName);
        
        if(null!=coverPath && !"".equals(coverPath)) {
            String videoTime = ConvertVideo.getMovieTime(fname);
            String toPath = fname.substring(0, fname.lastIndexOf("/"));
            Thread thread = new Thread(new SimpleThread(fname, toPath));
            thread.start();
            jsonObj.put("videoTime", videoTime);
            jsonObj.put("videoCoverPath", coverPath);
        }
        
        return jsonObj;
    }
}
